﻿using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Semantics.Silverlight;
using DanielVaughan;
using SilverlightParser.ServiceReference1;
namespace SilverlightParser
{
    public class RemotableMentorProxy
    {
        public RemotableMentorProxy()
        {
            ParserServiceClient = new ServiceReference1.ParserServiceClient();
            ParserServiceClient.GetSubCategoriesCompleted += new EventHandler<ServiceReference1.GetSubCategoriesCompletedEventArgs>(ParserServiceClient_GetSubCategoriesCompleted);
            ParserServiceClient.CreateWordCompleted += new EventHandler<ServiceReference1.CreateWordCompletedEventArgs>(ParserServiceClient_CreateWordCompleted);
            ParserServiceClient.SearchCategoriesCompleted += new EventHandler<ServiceReference1.SearchCategoriesCompletedEventArgs>(ParserServiceClient_SearchCategoriesCompleted);
            ParserServiceClient.GetCategroyHierarchyCompleted += new EventHandler<ServiceReference1.GetCategroyHierarchyCompletedEventArgs>(ParserServiceClient_GetCategroyHierarchyCompleted);
        }

        public delegate void GetCategroyHierarchyCompletedEventDelegate(List<Category> categories);
        public event GetCategroyHierarchyCompletedEventDelegate GetCategroyHierarchyCompletedEvent;
        void ParserServiceClient_GetCategroyHierarchyCompleted(object sender, ServiceReference1.GetCategroyHierarchyCompletedEventArgs e)
        {
            List<Category> cates = new List<Category>();
            cates.AddRange(e.Result);
            this.GetCategroyHierarchyCompletedEvent(cates);
        }

        public delegate void SearchCategoriesCompletedEventDelegate(List<Category> categories);
        public event SearchCategoriesCompletedEventDelegate SearchCategoriesCompletedEvent;
        void ParserServiceClient_SearchCategoriesCompleted(object sender, ServiceReference1.SearchCategoriesCompletedEventArgs e)
        {
            List<Category> cates = new List<Category>();
            cates.AddRange(e.Result);
            this.SearchCategoriesCompletedEvent(cates);
        }

        public delegate void CreateWordCompletedEventDelegate(int newwordid);
        public event CreateWordCompletedEventDelegate CreateWordCompletedEvent;
        void ParserServiceClient_CreateWordCompleted(object sender, ServiceReference1.CreateWordCompletedEventArgs e)
        {
            this.CreateWordCompletedEvent(e.Result);
        }

        void ParserServiceClient_GetSubCategoriesCompleted(object sender, ServiceReference1.GetSubCategoriesCompletedEventArgs e)
        {
            try
            {
                this.Event = e;
                List<Category> cates = new List<Category>();
                cates.AddRange(this.Event.Result);
                this.GetSubCategoriesCompletedEvent(cates);
            }
            catch (Exception exp)
            {
                this.ExceptionEvent(exp);
            }
        }

        public event ExceptionHandlerDelegate ExceptionEvent;

        public delegate void GetSubCategoriesCompletedEventHandler(List<Category> cates);
        public event GetSubCategoriesCompletedEventHandler GetSubCategoriesCompletedEvent;
        ServiceReference1.ParserServiceClient ParserServiceClient;
        ServiceReference1.GetSubCategoriesCompletedEventArgs Event;
        public List<Category> GetSubCategories(int nodeid)
        {
            var simpleService = ChannelManager.Instance.GetChannel<ParserService>();
            /* Perform synchronous WCF call. */
            ObservableCollection<Category> result = SynchronousChannelBroker.PerformAction<ObservableCollection<Category>, int>(
                simpleService.BeginGetSubCategories, simpleService.EndGetSubCategories, nodeid);
            List<Category> cates = new List<Category>();
            cates.AddRange(result);
            return cates;
        }

        public int CreateWord(string word, int categorynodeid)
        {
            var simpleService = ChannelManager.Instance.GetChannel<ParserService>();
            /* Perform synchronous WCF call. */
            int result = SynchronousChannelBroker.PerformAction<int, string, int>(
                simpleService.BeginCreateWord, simpleService.EndCreateWord, word, categorynodeid);
            return result;
        }
        public List<Category> SearchCategories(string word)
        {
            var simpleService = ChannelManager.Instance.GetChannel<ParserService>();
            /* Perform synchronous WCF call. */
            ObservableCollection<Category> result = SynchronousChannelBroker.PerformAction<ObservableCollection<Category>, string>(
                simpleService.BeginSearchCategories, simpleService.EndSearchCategories, word);
            List<Category> cates = new List<Category>();
            cates.AddRange(result);
            return cates;
        }
        public void GetCategroyHierarchy(int nodeid)
        {
            this.ParserServiceClient.GetCategroyHierarchyAsync(nodeid);
        }
    }
}
